package com.fd.dao.impl;

import com.fd.dao.StudentDao;
import com.fd.entity.Student;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao {
    @Override
    public void save(Student student) {
        //插入数据
        String sql = "insert into student (name , age ,gender ,address,classes) value(?,?,?,?,?)";
        getJdbcTemplate().update(sql, student.getName(), student.getAge(), student.getGender(), student.getAddress(), student.getClasses());
    }

    @Override
    public void delete(Integer sid) {
        //id删除
        String sql = "delete from student where sid = ?";
        getJdbcTemplate().update(sql, sid);
    }

    @Override
    public void update(Student student) {
        //修改信息
        String sql = "update student set name = ? ,age = ? ,gender= ? ,address= ? ,classes= ?   where sid = ?";
        getJdbcTemplate().update(sql, student.getName(), student.getAge(), student.getGender(), student.getAddress(), student.getClasses(), student.getsId());
    }

    @Override
    public List<Integer> getAllId() {
        //修改信息
        String sql = "select sid from student";
        List<Map<String, Object>> mapsStudentId = getJdbcTemplate().queryForList(sql);

        ArrayList<Integer> integers = new ArrayList<>();
        for (Map map:
        mapsStudentId) {
            System.out.println("map = " + map);
            Integer sid = (Integer)map.get("sid");
            System.out.println("sid = " + sid);
            integers.add(sid);
        }

        System.out.println("mapsStudentId = " + mapsStudentId);
        System.out.println("integers = " + integers);
        return integers;
    }

    @Override
    public Student getById(Integer id) {
        String sql = "select * from  student where sid = ? ";

        //直接查询
        // Role role = getJdbcTemplate().queryForObject(sql, new Object[]{id}, Role.class);
        Student student = (Student) getJdbcTemplate().queryForObject(sql, new Object[]{id}, new RowMapper<Object>() {

            public Object mapRow(ResultSet resultSet, int index) throws SQLException {
                //将一行数据解析成一个java对象
                return mapRowHandler(resultSet);
            }
        });
        return student;
    }

    @Override
    public List<Student> getAll() {
        String sql = "select * from student";
        List<Student> list = getJdbcTemplate().query(sql, new RowMapper<Student>() {
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                return mapRowHandler(resultSet);
            }
        });
        return list;
    }

    private Student mapRowHandler(ResultSet resultSet) throws SQLException {
        Student student = new Student();
        student.setsId(resultSet.getInt("sid"));
        student.setName(resultSet.getString("name"));
        student.setAge(resultSet.getInt("age"));
        student.setGender(resultSet.getString("gender"));
        student.setAddress(resultSet.getString("address"));
        student.setClasses(resultSet.getInt("classes"));

        return student;
    }

}
